package cn.tedu.blog.server.service.impl;

import cn.tedu.blog.server.exception.ServiceException;
import cn.tedu.blog.server.mapper.MessagePushMapper;
import cn.tedu.blog.server.pojo.dto.MessagePushNewAdd;
import cn.tedu.blog.server.pojo.entity.MessagePush;
import cn.tedu.blog.server.pojo.vo.MessagePushListVO;
import cn.tedu.blog.server.service.IMessagePushService;
import cn.tedu.blog.server.utils.JsonPage;
import cn.tedu.blog.server.utils.ServiceCode;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

@Service
@Slf4j
public class MessagePushServiceImpl implements IMessagePushService {

    @Autowired
    private MessagePushMapper messagePushMapper;


    @Override
    public void createMessage(MessagePushNewAdd messagePushNewAdd) {
        MessagePush messagePush = new MessagePush();
        BeanUtils.copyProperties(messagePushNewAdd,messagePush);
        messagePush.setGmtCreate(LocalDateTime.now());

        if (messagePushMapper.createMessage(messagePush)!=1){
            throw new ServiceException(ServiceCode.ERR_INSERT,"服务器忙，请稍后再试");
        }
    }



    @Override
    public void updateStatusById(MessagePushNewAdd messagePushNewAdd) {
        if (messagePushMapper.updateStatusById(messagePushNewAdd)!=1){
            throw new ServiceException(ServiceCode.ERR_INSERT,"服务器忙，请稍后再试");
        }
    }



    @Override
    public void updateStatusByUserId(Long userId) {
        messagePushMapper.updateStatusByUserId(userId);
    }



    @Override
    public void updateReadStatusAll(Long userId) {
        messagePushMapper.updateReadStatusAll(userId);
    }



    @Override
    public int ReadStatusCount(Long userId) {
        log.debug("一键已读功能，用户id={}",userId);
        return messagePushMapper.ReadStatusCount(userId);
    }



    @Override
    public int selectMessageByUserIdCountAll(Long userId) {
        log.debug("查询启用状态下所有推送消息的数量，用户id={}",userId);
        return messagePushMapper.selectMessageByUserIdCountAll(userId);
    }


    @Override
    public JsonPage<MessagePushListVO> selectMessageByUserId(MessagePushListVO messagePushListVO) {
        log.debug("查询推送信息，参数={}",messagePushListVO);
        PageHelper.startPage(messagePushListVO.getPageNum(),messagePushListVO.getPageSize());
        return JsonPage.restPage(new PageInfo<>(messagePushMapper.selectMessageByUserId(messagePushListVO)));
    }
}
